﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.11f1
 *Date:           2019-04-06
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System;

namespace IFramework
{
    public class ABAsset
    {
        public int ReferenceCout { get; private set; }
        public virtual UnityEngine.Object Asset { get; protected set; }
        public string AssetPath { get; private set; }
        public Type AssetType { get; private set; }
        public virtual bool IsDone { get { return true; } }
        public Action<ABAsset> OnComplete;
        public ABAsset(string path, Type type)
        {
            AssetPath = path;
            AssetType = type;

        }
        public void Load() { OnLoad(); }
        public virtual void OnLoad()
        {
#if UNITY_EDITOR
            Asset = UnityEditor.AssetDatabase.LoadAssetAtPath(AssetPath, AssetType);
#endif
        }
        public void UnLoad() {

            Asset = null;
            OnUnLoad();
            AssetPath = null;
        }
        public virtual void OnUnLoad() { }
        public bool Update()
        {
            if (IsDone)
            {
                if (OnComplete!=null)
                {
                    OnComplete(this);
                    OnComplete = null;
                }
                return false;
            }
            return true;
        }
        public void Retain()
        {
            Update();
            ReferenceCout++;
        }

        public void Release()
        {
            if (--ReferenceCout < 0)
            {
                Log.E("ABAsset refCount < 0");
            }
        }
    }
   
}
